package com.hjw.weixun.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hjw.weixun.domain.User;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author henry_chen date 2011-8-2 action的总类
 * 
 * 
 */

public class GenericAction extends ActionSupport implements StrutsStatics,
		ServletRequestAware, ServletResponseAware {
	/**
	 * 序列号
	 */

	private static final long serialVersionUID = -5261970713675901712L;

	private static final Logger logger = LoggerFactory
			.getLogger(GenericAction.class);

	private HttpServletRequest request;

	private HttpServletResponse response;

	private String filePath;

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = ServletActionContext.getServletContext()
				.getInitParameter("sampleImageFolder");
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	/**
	 * @param response
	 * @param text
	 */
	protected void renderHtml(String text) {
		try {
			response.setContentType("application/json;charset=UTF-8");
			response.getWriter().write(text);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 返回Json的true,或者False信息
	 * 
	 * @param result
	 */
	protected String renderResult(boolean result) {
		if (result) {
			renderHtml("{\"success\":\"true\"}");
		} else {
			renderHtml("{\"success\":\"false\"}");

		}
		return null;
	}

	/**
	 * 从Session中获取用户
	 * 
	 * @return
	 */
	protected User getCurrentUser() {

		// Session中获取用户
		Object userObj = request.getSession().getAttribute("user");

		if (null == userObj) {
			return null;
		}
		return (User) userObj;

	}

	protected String getBasePath() {

		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + path + "/";
		return basePath;

	}

}
